package com.xudow.app.dynamicstate_old.module.user;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.jude.beam.bijection.Presenter;
import com.jude.library.imageprovider.ImageProvider;
import com.jude.library.imageprovider.OnImageSelectListener;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.widget.ProgressDialogTransform;

import java.io.File;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by ske on 2016/6/16.
 */
public class PhotoChangePresenter extends Presenter<PhotoChangeActivity> {
    private Uri avatar = null;
    private ImageProvider provider;
    boolean isAvatar;

    OnImageSelectListener listener = new OnImageSelectListener() {

        @Override
        public void onImageSelect() {
            getView().getExpansion().showProgressDialog("加载中");
        }

        @Override
        public void onImageLoaded(Uri uri) {
            getView().getExpansion().dismissProgressDialog();
            int sizeH = 0;
            int sizeW = 0;
            if (isAvatar)
                sizeH = sizeW = 200;
            else {
                sizeW = 600;
                sizeH = 400;
            }
            provider.corpImage(uri, sizeW, sizeH, new OnImageSelectListener() {
                @Override
                public void onImageSelect() {

                }

                @Override
                public void onImageLoaded(Uri uri) {
                    getView().getExpansion().dismissProgressDialog();
                    getView().setImage(uri);
                    avatar = uri;
                }

                @Override
                public void onError() {

                }
            });
        }

        @Override
        public void onError() {

        }
    };

    @Override
    protected void onCreate(PhotoChangeActivity view, Bundle savedState) {
        super.onCreate(view, savedState);
        provider = new ImageProvider(getView());
        isAvatar = getView().getIntent().getBooleanExtra("isAvatar", true);
    }

    public void editFace(int style) {
        switch (style) {
            case 0:
                provider.getImageFromCamera(listener);
                break;
            case 1:
                provider.getImageFromAlbum(listener);
                break;
        }
    }

    @Override
    protected void onResult(int requestCode, int resultCode, Intent data) {
        super.onResult(requestCode, resultCode, data);
        provider.onActivityResult(requestCode, resultCode, data);
    }

    public void changeCover() {
        if (avatar != null) {
            if (isAvatar) {
                File file = new File(avatar.getPath());
                ImageModel.getInstance().putImageSync(file)
                        .flatMap((Func1<String, Observable<String>>) s -> DynamicModel.getInstance().changeAvatar(s, file))
                        .compose(new ProgressDialogTransform<>(getView(), "上传中"))
                        .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                        .subscribe(message -> {
                            Toast.makeText(getView(), "修改成功", Toast.LENGTH_SHORT).show();
                            getView().setResult(Activity.RESULT_OK);
                            getView().finish();
                        });
            } else
                ImageModel.getInstance().putImageSync(new File(avatar.getPath()))
                        .flatMap((Func1<String, Observable<String>>) s -> DynamicModel.getInstance().changeCover(s))
                        .compose(new ProgressDialogTransform<>(getView(), "上传中"))
                        .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                        .subscribe(message -> {
                            Toast.makeText(getView(), "修改成功", Toast.LENGTH_SHORT).show();
                            getView().setResult(Activity.RESULT_OK);
                            getView().finish();
                        });
        }
    }
}
